{# Copyright (c) 2014-2017, NVIDIA CORPORATION.  All rights reserved. #}

{% macro print_flashes() %}
    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
            {% for category, message in messages %}
                <div class="alert alert-{{ category }}">
                    {{ message | safe}}
                </div>
            {% endfor %}
        {% endif %}
    {% endwith %}
{% endmacro %}

{% macro print_errors(form) %}
    {% if form.errors %}
        <div class="alert alert-danger">
            {% for field_name, field in form._fields.iteritems() %}
                {% if field.errors %}
                    <li><label>{{ field.label.text }}</label>
                        <ul>
                            {% for e in field.errors %}
                                <li>{{ e }}</li>
                            {% endfor %}
                        </ul>
                    </li>
                {% endif %}
            {% endfor %}
        </div>
    {% endif %}
    {% if form.warnings %}
        <div class="alert alert-warning">
            {% for field_name, field in form._fields.iteritems() %}
                {% if field.warnings %}
                    <li><label>{{ field.label.text }}</label>
                        <ul>
                            {% for e in field.warnings %}
                                <li>{{ e }}</li>
                            {% endfor %}
                        </ul>
                    </li>
                {% endif %}
            {% endfor %}
        </div>
    {% endif %}
{% endmacro %}

{% macro print_combined_errors(errors, warnings) %}
    {% if errors %}
        <div class="alert alert-danger">
            {% for field_name, field in errors.iteritems() %}
                <li><label>{{ field_name }}</label>
                    <ul>
                        {% for e in field %}
                            <li>{{ e }}</li>
                        {% endfor %}
                    </ul>
                </li>
            {% endfor %}
        </div>
    {% endif %}
    {% if warnings %}
        <div class="alert alert-warning">
            {% for field_name, field in warnings.iteritems() %}
                <li><label>{{ field.label.text }}</label>
                    <ul>
                        {% for e in field %}
                            <li>{{ e }}</li>
                        {% endfor %}
                    </ul>
                </li>
            {% endfor %}
        </div>
    {% endif %}
{% endmacro %}

{% macro mark_errors(list) %}
    {% for form in list %}
        {{ ' has-error' if form.errors else '' }}
        {{ ' has-warning' if form.warnings else '' }}
    {% endfor %}
{% endmacro %}

{% macro serve_file(obj, path) %}
    {% if path[0] == '/' %}
        {# Absolute path - don't serve file for security reasons #}
        {{path}}
    {% else %}
        <a href="{{url_for('digits.views.serve_file', path=obj.path(path, relative=True))}}">{{path}}</a>
    {% endif %}
{% endmacro %}
